package org.chench.extra.logsift.advice;

import org.chench.extra.logsift.vo.ResultVO;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * 全局异常拦截器
 * @author chench
 * @date 2025.05.17
 */
@ControllerAdvice
public class GlobalExceptionAdvice {
    private static final Logger logger = LoggerFactory.getLogger(GlobalExceptionAdvice.class);

    @ResponseBody
    @ExceptionHandler(value = Exception.class)
    public Object handleException(HttpServletRequest req, HttpServletResponse resp, Exception e) {
        logger.error("handle ex: {}, request uri: {}", e.getMessage(), req.getRequestURI(), e);
        return ResultVO.buildError();
    }
}
